#include "LD3320_Config.h"
#include <unistd.h>

extern UBYTE nLD_Mode;// Record the status of LD3320(ASR or Play Sound)
extern UBYTE bMp3Play;// Record the status of audio player

/******************************************************************************
									GPIO
******************************************************************************/
void DEV_Digital_Write(UWORD Pin, UBYTE Value){
    digitalWrite(Pin, Value);
}

UBYTE DEV_Digital_Read(UWORD Pin){
    UBYTE Read_value = 0;

    Read_value = digitalRead(Pin);
   
    return Read_value;
}

void DEV_GPIO_Mode(UWORD Pin, UWORD Mode){
    if(Mode == 0 || Mode == INPUT){
        pinMode(Pin, INPUT);
        pullUpDnControl(Pin, PUD_UP);
    }else{ 
        pinMode(Pin, OUTPUT);
        // printf (" %d OUT \r\n",Pin);
    }
}

void DEV_GPIO_Init(void){
    DEV_GPIO_Mode(LD_CS, 1);	
	DEV_GPIO_Mode(LD_WR, 1);
    DEV_GPIO_Mode(LD_RST, 1);
	DEV_GPIO_Mode(LD_IRQ, 0);
}

/******************************************************************************
function:	Module Initialize, the library and initialize the pins, SPI protocol
******************************************************************************/
UBYTE DEV_ModuleInit(void){
    if(wiringPiSetup() < 0) {//use wiringpi Pin number table  
    //if(wiringPiSetupGpio() < 0) { //use BCM2835 Pin number table
        printf("LD3320_Config:DEV_ModuleInit: set wiringPi lib failed	!!! \r\n");
        return 1;
    } else {
        printf("LD3320_Config:DEV_ModuleInit: set wiringPi lib success  !!! \r\n");
    }
	DEV_GPIO_Init();
	printf("LD3320_Config:DEV_ModuleInit: USE_SPI\r\n");
	wiringPiSPISetupMode(0, 1000000, 2);
    return 0;
}

/********************************************************************************
function:	
				SPI send data
********************************************************************************/
UBYTE spi_send_byte(UBYTE Value){
	wiringPiSPIDataRW(0,&Value,1);//spi0通道 1个字节
    return Value;
}

/********************************************************************************
function:		
				Delay 
********************************************************************************/
void Driver_Delay_ms(uint32_t xms){
    delay(xms);
}

/******************************************************************************
function:	Module exits, closes SPI and BCM2835 library
parameter:
Info:
******************************************************************************/
void DEV_ModuleExit(void){
    CS_0;
	RST_1;
	WR_0;
}
